package com.gable.report.controller;


import com.alibaba.fastjson.JSON;
import com.gable.report.common.util.HttpHelper;
import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.gable.report.util.HttpProcessUtil.getAccessPath;
import static com.gable.report.util.HttpProcessUtil.headerList;

/**
 * @Author: 杨青桦
 * @Description:  具体设备卡片维修详情
 * @Date: 2018/4/23 11:03
 */
@Controller
@RequestMapping("charts/assets")
public class ChartsForAssetsRepairDetailController {

    private final Log logger = LogFactory.getLog(this.getClass());

    private static final String rootPath = "charts/assets/";

    @Autowired
    HttpServletRequest request;

    @Value("${hospital.address}")
    String hospitalAddress;


    /**
     * @Author: 杨青桦
     * @Description:  设备总览页面
     * @Date: 2017/12/4 10:49
     */
    @RequestMapping(value="repair/page")
    public String page(){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user == null){
            return "login";
        }
        return "charts/assets/repair-detail";
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询派工详情
     * @Date: 2018/4/23 11:52
     * @param id
     * @param dispatchId
     * @return
     */
    @RequestMapping(value="repair/getRepairList",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil getRepairList(String id, //维修id
                                          String dispatchId) { //派工
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id", id);
            paramMap.put("dispatchId", dispatchId);
            String path = rootPath + "repair/getRepairList";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            if (StringUtils.isEmpty(result)) {
                return new ReturnResultUtil(0, "网络异常", result);
            }
            return JSON.parseObject(result, ReturnResultUtil.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0, "网络异常");
        }

    }

    /**
     * @Author: 杨青桦
     * @Description: 查询维修时间轴
     * @Date: 2018/4/23 12:57
     * @param repairId
     * @return
     */
    @RequestMapping(value="repair/queryRepairTimeLine",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String queryRepairTimeLine(String repairId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId",repairId);
            String path = rootPath + "repair/queryRepairTimeLine";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询派工信息详情
     * @Date: 2018/4/23 13:01
     * @param dispatchId
     * @return
     */
    @RequestMapping(value="repair/getDispatchVisitList",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String getDispatchVisitList(String dispatchId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("dispatchId",dispatchId);
            String path = rootPath + "repair/getDispatchVisitList";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询维修时上传的文件信息(暂时不用)
     * @Date: 2018/4/23 13:16
     * @param repairId
     * @return
     */
    @RequestMapping(value="repair/queryFileRepair",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String queryFileRepair(String repairId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId",repairId);
            String path = rootPath + "repair/queryFileRepair";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询派工时上传的文件信息
     * @Date: 2018/4/23 13:16
     * @param repairId
     * @return
     */
    @RequestMapping(value="repair/queryFileDispatch",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String queryFileDispatch(String repairId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId",repairId);
            String path = rootPath + "repair/queryFileDispatch";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询维修费用详情
     * @Date: 2018/4/23 13:55
     * @param id
     * @param dispatchId
     * @return
     */
    @RequestMapping(value="repair/queryCostList",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String queryDispatchCostList(String id,String dispatchId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id",id);
            paramMap.put("dispatchId",dispatchId);
            String path = rootPath + "repair/queryCostList";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询评价信息
     * @Date: 2018/4/23 14:00
     * @param repairId
     * @return
     */
    @RequestMapping(value="upkeep/queryComment",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public String queryComment(String repairId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId",repairId);
            String path = rootPath + "upkeep/queryComment";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询报价信息
     * @Date: 2018/4/23 14:06
     * @param repairId
     * @return
     */
    @RequestMapping(value="offer/list",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil list(String repairId) {
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId", repairId);
            String path = rootPath + "offer/list";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return JSON.parseObject(result, ReturnResultUtil.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0, "网络异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询备件信息
     * @Date: 2018/4/23 14:43
     * @param repairId
     * @return
     */
    @RequestMapping(value="repair/queryDispatchPart",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody String queryDispatchPart(String repairId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId",repairId);
            String path = rootPath + "repair/queryDispatchPart";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  在此只用于查询故障图片
     * @Date: 2018/4/23 14:50
     * @param id
     * @return
     */
    @RequestMapping(value="repair/queryRepairView",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody String queryRepairView(String id){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id",id);
            String path = rootPath + "repair/queryRepairView";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备历史维修记录
     * @Date: 2018/4/23 14:46
     * @param assetsId
     * @return
     */
    @RequestMapping(value="repair/queryRepairAssets",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody String queryRepairAssets(String assetsId){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("assetsId",assetsId);
            String path = rootPath + "repair/queryRepairAssets";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询合同ID
     * @Date: 2018/4/23 14:58
     * @param repairId
     * @return
     */
    @RequestMapping(value="dispatchingAssets/queryContractInfoId",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryContractInfoId(String repairId) {
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("repairId", repairId);
            String path = rootPath + "dispatchingAssets/queryContractInfoId";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return JSON.parseObject(result, ReturnResultUtil.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0, "网络异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  根据维修合同id获取合同信息，维修合同下的资产信息
     * @Date: 2018/4/23 15:02
     * @param id
     * @return
     */
    @RequestMapping(value="contract/get",method=RequestMethod.GET,produces="application/json;charset=utf-8")
    public @ResponseBody String get(String id){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id",id);
            String path = rootPath + "contract/get";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 查询备件单个对象
     * @Date: 2018/4/23 15:09
     * @param id
     * @return
     */
    @RequestMapping(value="repair/getDispatchPart",method=RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody String getDispatchPart(String id){
        String result;
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id",id);
            String path = rootPath + "repair/getDispatchPart";    //接口路径
            String url = getAccessPath(hospitalAddress, path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList, url, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new ReturnResultUtil(0, "网络异常"));
        }
    }
}
